package GDoc::Job::Fetch::US::Exec::Ag::NASS;

# Most of the NASS reports have the same structure, so we
# can use a generic run method and the subclasses only
# need to define the baseurl and store methods

use Moose;
use Scalar::Util 'blessed';


extends 'GDoc::Job::Fetch';

sub run { 
    my $self = shift;
   
    if ( $self->data->{link} ) { 
        $self->_fetch_pdf;
    } else { 
        $self->logger->info( "fetching base " . $self->_baseurl );
        my $mech = $self->_mech;
        $mech->get( $self->_baseurl );
        my @links = $mech->find_all_links( url_regex => qr{-\d{2}-\d{2}-\d{4}\.pdf$}i );

        foreach my $link( @links ) { 
            $link->url =~ m{(?<month>\d{2})-(?<day>\d{2})-(?<year>\d{4})\.pdf$};
            my $day   = $+{day};
            my $month = $+{month};
            my $year  = $+{year};
            
            my $jclass = blessed $self;
            $jclass =~ s/GDoc::Job:://;
            my $job = GDoc::Job->new( class => $jclass,
                                      queue => $self->fetch_queue,
                                      data  => { link   => $link->url,
                                                 day    => $day,
                                                 month  => $month,
                                                 year   => $year
                                               }
                                    );
            $job->submit;
        }
    }
}



1;
